package com.bub.pay.outer.redis;

import jodd.mail.Email;
import jodd.mail.SendMailSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AsyncEmailService implements Runnable {

	/**
	 * Logger
	 */
	private static final Logger log = LoggerFactory.getLogger(AsyncEmailService.class);

	private final SendMailSession sendMailSession;
	private final Email email;

	public AsyncEmailService(final SendMailSession sendMailSession, final Email email) {
		this.sendMailSession = sendMailSession;
		this.email = email;
	}

	private boolean isEmptyParam() {
		return null == email || null == sendMailSession;
	}

	@Override
	public void run() {
		if (isEmptyParam()) {
			log.error("AsyncEmailService parameter must not be null");
			return;
		}
		try {
			if (!sendMailSession.isConnected())
				sendMailSession.open();
			sendMailSession.sendMail(email);
		} catch (Exception ex) {
			log.error("AsyncEmailService send email error:", ex);
		}

	}

}
